﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Auditing;
using Abp.Web.Models;
using FwProject.Configuration;
using FwProject.Controllers;
using FwProject.EventAdvertising.Dto;
using FwProject.Shop;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace FwProject.Web.Shop.Controllers
{
    /// <summary>
    /// 促销管理
    /// </summary>
    public class PromotionsController : FwProjectControllerBase
    {
        private readonly IShopSecondkillAppService _shopSecondkillAppService;
        private readonly IShopProductAppService _shopProductAppService;

        private readonly IConfigurationRoot _appConfiguration;
        public PromotionsController(IShopSecondkillAppService shopSecondkillAppService, IShopProductAppService shopProductAppService, IHostingEnvironment env)
        {
            _shopSecondkillAppService = shopSecondkillAppService;
            _shopProductAppService = shopProductAppService;
            _appConfiguration = env.GetAppConfiguration();
        }

        /// <summary>
        /// 秒杀活动列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetSpikeData(int page, int limit, string keyword = "", string range = "")
        {
            var data = await _shopSecondkillAppService.GetSpikeLayerDataAsync(page, limit, keyword, range);

            return Json(data);
        }


        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Create(ShopSecondkillBackstageEnterDto input)
        {
            await _shopSecondkillAppService.CreateBatchesAsync(input);
            return Json("0");
        }

        public async Task<IActionResult> Edit(Guid id)
        {
            ViewBag.Id = id;
            var entity = await _shopSecondkillAppService.GetEntityByIdAsync(id);
            return View(entity);
        }

        [HttpPost]
        public async Task<IActionResult> Edit(Guid id, ShopSecondkillBackstageEnterDto input)
        {
            await _shopSecondkillAppService.ModifyAsync(id, input);
            return Json(id);
        }

        public async Task<IActionResult> Delete(Guid id)
        {
            await _shopSecondkillAppService.DeleteShopSecondkill(id);
            return Json(id);
        }

        public IActionResult ShopProductSelect()
        {
            ViewBag.ImageUrl = _appConfiguration["ImageUrl:url"].ToString();
            return View();
        }

        public async Task<IActionResult> GetShopProductData(int page, string keyword = "")
        {
            var data = await _shopProductAppService.GetPageListAsync(page, 10, keyword);
            return Json(new { data=data, hasNextPage = data.HasNextPage });
        }
    }
}